package org.springcloud.wisdom.spring_01_canteen.indent;

import org.springcloud.wisdom.spring_01_canteen.common.bean.ResponseData;
import org.springcloud.wisdom.spring_01_canteen.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/indent")
public class IndentController extends BaseController {
    private final IndentService indentService;

    @Autowired
    public IndentController(IndentService indentService) {
        this.indentService = indentService;
    }

    // 添加订单
    @PostMapping("/add")
    public ResponseEntity<String> addIndent(@RequestBody Indent indent) {
        String indentId = indentService.addIndent(indent);
        if (indentId != null) {
            System.out.println("生成订单:" + indent);
            return ResponseEntity.ok(indentId);
        } else {
            return ResponseEntity.badRequest().body(String.valueOf("添加订单失败"));
        }
    }

    // 查看订单
    @GetMapping("/getAll")
    public ResponseEntity<List<Indent>> getAllIndents(@RequestParam(value = "mallid",required = false) Integer mallid) {
        List<Indent> indents = indentService.getAllIndents(mallid);
        return ResponseEntity.ok(indents);
    }

    //修改订单状态
    @PutMapping("/updatesign")
    public ResponseData<Boolean> updatesign(@RequestBody Indent indent){
        return returnBoolean(indentService.updatesign(indent));
    }

    // 获取所有订单的数量
    @GetMapping("/count")
    public int getAllOrderCount() {
        return indentService.getAllOrderCount();
    }

    // 根据日期获取订单数量
    @GetMapping("/count/date")
    public int getOrderCountByDate(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                   @RequestParam(value = "mallid",required = false) Integer mallid) {
        return indentService.getOrderCountByDate(date,mallid);
    }

    //商家营业额
    @GetMapping("/sum/totalprice")
    public BigDecimal getSumTotalPriceByMallId(@RequestParam Integer mallid) {
        return indentService.sumTotalPriceByMallId(mallid);
    }
}
